package com.dennytech.tac.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dennytech.tac.base.CFFragment;
import com.dennytech.tac.model.CityIntro;
import com.dennytech.tac.statistics.Stat;
import com.dennytech.tac.utils.db.CityIntroDBHelper;
import com.dennytech.tac.R;

public class HomeFragment extends CFFragment implements OnClickListener {

	private Button titleBtn;

	private ImageView cityIcon;
	private ImageView userIcon;
	private LinearLayout userLay;
	private LinearLayout userLay2;
	private TextView userIntro;
	private TextView userReview;
	private Button startBtn;

	private LinearLayout menusLay;
	private View menu1;
	private View menu2;

	private CityIntro cityIntro;

	private Animation startAnim, reviewHideAnim;
	private Animation showMenuAnim;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.home_fragment, container, false);

		// title button
		titleBtn = (Button) view.findViewById(R.id.title_button);
		titleBtn.setOnClickListener(this);

		cityIcon = (ImageView) view.findViewById(R.id.home_headimg);

		userLay = (LinearLayout) view.findViewById(R.id.home_user_lay);
		userLay2 = (LinearLayout) view.findViewById(R.id.home_user_lay2);
		userIcon = (ImageView) view.findViewById(R.id.home_user_avatar);
		userIntro = (TextView) view.findViewById(R.id.home_user_intro);
		userReview = (TextView) view.findViewById(R.id.home_user_review);
		startBtn = (Button) view.findViewById(R.id.home_start_btn);
		startBtn.setOnClickListener(this);

		menusLay = (LinearLayout) view.findViewById(R.id.home_menus_lay);
		menu1 = view.findViewById(R.id.home_menu_1);
		menu2 = view.findViewById(R.id.home_menu_2);
		menu1.setOnClickListener(this);
		menu2.setOnClickListener(this);

		return view;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setTitle(getActivity().getResources().getString(R.string.app_name));

		CityIntroDBHelper cis = CityIntroDBHelper.instance();
		cityIntro = cis.getCityIntro("Shanghai");

		cityIcon.setImageBitmap(cityIntro.getBigIcon());
		userIcon.setImageBitmap(cityIntro.getReviewerIcon());
		userIntro.setText(cityIntro.getReviewerName());
		userReview
				.setText("  I have been living in Shanghai for two years after graduated from college, "
						+ "Shanghai is a beautiful city, ancient and rich modern beauty, "
						+ "not only be able to see a few centuries alley here, and also be able to see the modern "
						+ "east pearl tower. I love this city.");

		setUpAnimation();
		checkFirstOpenTheCity();
	}

	private void setUpAnimation() {
		startAnim = AnimationUtils.loadAnimation(getActivity(),
				R.anim.home_start);
		startAnim.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				hideUserInfo(false);
				menusLay.startAnimation(showMenuAnim);
			}
		});

		reviewHideAnim = AnimationUtils.loadAnimation(getActivity(),
				R.anim.home_start);

		showMenuAnim = AnimationUtils.loadAnimation(getActivity(),
				R.anim.home_show_menu);
	}

	private void checkFirstOpenTheCity() {
		SharedPreferences pref = preferences(getActivity());
		boolean isFirst = pref.getBoolean("firstTimeOpenApp", true);
		if (isFirst) {
			showUserInfo(false);
			pref.edit().putBoolean("firstTimeOpenApp", false).commit();
		} else {
			hideUserInfo(false);
		}
	}

	private void showUserInfo(boolean hasAnimate) {
		if (hasAnimate) {

		} else {

		}
		userLay.setVisibility(View.VISIBLE);
		userReview.setVisibility(View.VISIBLE);
		startBtn.setVisibility(View.VISIBLE);

		menusLay.setVisibility(View.GONE);
	}

	private void hideUserInfo(boolean hasAnimate) {
		if (hasAnimate) {
			userLay.startAnimation(startAnim);
			userLay2.startAnimation(reviewHideAnim);
		} else {
			userLay.setVisibility(View.GONE);
			userLay2.setVisibility(View.GONE);

			menusLay.setVisibility(View.VISIBLE);
		}
	}

	private void switchCity() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setTitle(getString(R.string.message_switch_city_title));
		builder.setMessage(getString(R.string.message_switch_city_msg));
		builder.setPositiveButton(android.R.string.ok,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});
		AlertDialog dlg = builder.create();
		dlg.show();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.home_menu_1) {
			Intent i = new Intent(Intent.ACTION_VIEW,
					Uri.parse("denny://talist"));
			startActivity(i);
			Stat.event("home", "home_attractions", null, 0);

		} else if (v.getId() == R.id.home_menu_2) {
			Intent i = new Intent(Intent.ACTION_VIEW,
					Uri.parse("denny://tglist"));
			startActivity(i);
			Stat.event("home", "home_travelguides", null, 0);

		} else if (v.getId() == R.id.home_start_btn) {
			hideUserInfo(true);
			Stat.event("home", "home_start", null, 0);

		} else if (v.getId() == R.id.title_button) {
			switchCity();
			Stat.event("home", "home_titlebtn", null, 0);
		}
	}
}
